package com.project.decoration.controller.partner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationPartner;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationPartnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 合作伙伴controller
 * @date 2025/02/14 03:05
 */
@Controller
@ResponseBody
@RequestMapping("partner")
public class DecorationPartnerController {

    @Autowired
    private DecorationPartnerService decorationPartnerService;

    /** 分页获取合作伙伴 */
    @PostMapping("getDecorationPartnerPage")
    public Result getDecorationPartnerPage(@RequestBody DecorationPartner decorationPartner) {
        Page<DecorationPartner> page = new Page<>(decorationPartner.getPageNumber(),decorationPartner.getPageSize());
        QueryWrapper<DecorationPartner> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationPartner.getName()),DecorationPartner::getName,decorationPartner.getName());
        Page<DecorationPartner> decorationPartnerPage = decorationPartnerService.page(page, queryWrapper);
        return Result.success(decorationPartnerPage);
    }

    /** 根据id获取合作伙伴 */
    @GetMapping("getDecorationPartnerById")
    public Result getDecorationPartnerById(@RequestParam("id")String id) {
        DecorationPartner decorationPartner = decorationPartnerService.getById(id);
        return Result.success(decorationPartner);
    }

    /** 保存合作伙伴 */
    @PostMapping("saveDecorationPartner")
    public Result saveDecorationPartner(@RequestBody DecorationPartner decorationPartner) {
        boolean save = decorationPartnerService.save(decorationPartner);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑合作伙伴 */
    @PostMapping("editDecorationPartner")
    public Result editDecorationPartner(@RequestBody DecorationPartner decorationPartner) {
        boolean save = decorationPartnerService.updateById(decorationPartner);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除合作伙伴 */
    @GetMapping("removeDecorationPartner")
    public Result removeDecorationPartner(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationPartnerService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("合作伙伴id不能为空！");
        }
    }

}
